<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="问题筛选"></uni-navbar></view>

		<view class="filter-box">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="问题标题">
						<!-- block -->
						<u-input v-model="clue.title" type="text" placeholder="请输入问题标题" />
					</u-form-item>
					<u-form-item class="form-item" label="镇街/市直代表小组" labelWidth="300">
						<u-input v-model="dept" type="select" :select-open="deptShow" placeholder="请选择类型" @click="deptShow = true" />
						<u-select :list="deptList" v-model="deptShow" @confirm="deptConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="提交人">
						<!-- block -->
						<u-input v-model="clue.submitUser" type="text" placeholder="请输入提交人" />
					</u-form-item>
					<u-form-item class="form-item" label="选择代表">
						<!-- block -->
						<u-input v-model="clue.userName" type="text" placeholder="请输入代表" />
					</u-form-item>
					<u-form-item class="form-item" label="开始时间">
						<!-- block -->
						<date-time-picker placeholder="请选择开始时间" :defaultValue="startTime" :old="true" fields="day" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="结束时间">
						<!-- block -->
						<date-time-picker placeholder="请选择结束时间" :defaultValue="endTime" :old="true" fields="day" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			show: false,
			title: '',

			dept: '',
			deptShow: false,
			deptList: [],

			startTime: '',
			endTime: '',

			clue: {
				title: '',
				submitUser: '',
				dateRange: '',
				userName: '',
				createName: '',
				deptId: '',
				nodeStatus: ''
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.loadDept();
		this.loadDict('represent_type', 'tabsList');
	},
	computed: {
		dateRangeSea: {
			get: function() {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function(e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				// this.$logout();
			}
		},
		loadData(type = 'add') {
			let pre = this.$prePage();
			if (pre && pre.clue) {
				pre.clue = this.clue;
			}
			if (pre && pre.loadData) {
				pre.loadData('refresh');
			}
			uni.navigateBack();
		},
		loadDictType(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type,
					remark: 'type'
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				res.data.forEach((item) => {
					this[list].push({
						name: item.dictValue,
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				if (list == 'tabsList') {
					this.tabsList.push({
						name: '全部',
						value: ''
					});
				}

				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName,
						name: item.dictName
					});
				});
			});
		},
		loadDept() {
			this.$request('/system/sys-dept/depts', {
				clientCode: this.clientCode,
				jiedao: '1'
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this.deptList = [];
				res.data.forEach((item) => {
					this.deptList.push({
						id: item.id,
						value: item.id,
						label: item.deptName,
						name: item.deptName
					});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		deptConfirm(e) {
			this.dept = e[0].label;
			this.clue.deptId = e[0].value;
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.typeFunc(this.tabsList[index].value);
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.clue.dateRange = this.dateRangeSea;

			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.clue.title = '';
			this.dept = '';
			this.clue.deptId = '';
			this.clue.submitUser = '';
			this.clue.dateRange = '';
			this.clue.userName = '';
			this.loadData('refresh');
		},
		search(e) {
			this.clue.title = e;
			this.loadData('refresh');
		},
		typeFunc(e) {
			this.clue.nodeStatus = e;
			this.loadData('refresh');
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
}

.filter-box {
	position: static;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;
	}
}
</style>
